package com.vcredit.bird.service.product;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.vcredit.bird.api.ICreditProductService;
import com.vcredit.bird.vo.product.ddq.CreditDdqDecisionResult;
import com.vcredit.creditcat.exception.ProductCanNotApplyException;
import com.vcredit.creditcat.vo.CommonResultExt;
import com.vcredit.framework.annotation.Log;
import com.vcredit.framework.exception.BaseException;
import com.vcredit.framework.util.JacksonUtil;

/**
 * @author 张新功
 */
@Service("creditDdqService")
public class CreditDdcashService implements ICreditProductService {
  @Value("${creditddq.accede}")
  private String creditDdqUrl;

  @Log
  @Override
  public Object commit(Object submitDto) throws ProductCanNotApplyException, JsonParseException,
      JsonMappingException, IOException, Exception {
    CreditDdqDecisionResult resultDto = null;
    //    String resultJson =
    //        HttpRequest.doJson(creditDdqUrl.concat("/FNGetCredit"),
    //            KakEncodeUtil.encodeJsonParam(JacksonUtil.Obj2Json(submitDto)));
    //    String decodeJsonParam = KakEncodeUtil.decodeJsonParam(resultJson);
    String decodeJsonParam =
        "{\"status\":\"1\",\"msg\":\"授信成功\",\"data\":{\"CreditStatus\":1,\"CreditAmount\":\"60000\",\"AvailableAmount\":\"20000\",\"RejectExpire\":\"2017-09-01 12:00:00\",\"CreditRemark\":\"\"}}";
    CommonResultExt<CreditDdqDecisionResult> result =
        JacksonUtil.Json2Obj(decodeJsonParam,
            new TypeReference<CommonResultExt<CreditDdqDecisionResult>>() {});
    if (!"1".equals(result.getStatus())) {
      throw new BaseException(result.getMsg());
    }
    resultDto = result.getData();
    return resultDto;
  }

  @Log
  @Override
  public Object getCredit(Object submitDto) throws ProductCanNotApplyException, Exception {
    CreditDdqDecisionResult resultDto = null;
    //    String resultJson =
    //        HttpRequest.doJson(creditDdqUrl.concat("/FNCheckCredit"),
    //            KakEncodeUtil.encodeJsonParam(JacksonUtil.Obj2Json(submitDto)));
    //    String decodeJsonParam = KakEncodeUtil.decodeJsonParam(resultJson);
    String decodeJsonParam =
        "{\"status\":\"1\",\"msg\":\"授信成功\",\"data\":{\"CreditStatus\":1,\"CreditAmount\":\"60000\",\"AvailableAmount\":\"20000\",\"RejectExpire\":\"2017-09-01 12:00:00\",\"CreditRemark\":\"\"}}";
    CommonResultExt<CreditDdqDecisionResult> json2Obj =
        JacksonUtil.Json2Obj(decodeJsonParam,
            new TypeReference<CommonResultExt<CreditDdqDecisionResult>>() {});
    if (!"1".equals(json2Obj.getStatus())) {
      throw new BaseException(json2Obj.getMsg());
    }
    resultDto = json2Obj.getData();
    return resultDto;
  }

  @Override
  public Object checkCredit(Object submitDto) throws ProductCanNotApplyException, Exception {
    return null;
  }

  //  @Override
  //  public void noticeApplyResult(ProductDecisionResultDto paramDto) throws Exception {
  //    String resultJson =
  //        HttpRequest.doJson(productAsync.concat("/creditDdq/noticeApplyResult"),
  //            JacksonUtil.Obj2Json(paramDto));
  //    CommonResultExt<String> result =
  //        JacksonUtil.Json2Obj(resultJson, new TypeReference<CommonResultExt<String>>() {});
  //    if (!"000".equals(result.getStatus())) {
  //      throw new BaseException(result.getMessage());
  //    }
  //  }

}
